#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include "rngs.h"

#define NUM_TESTS 30
int RANDOM_PLAYER_COUNT_EVERYWHERE= rand()%(MAX_PLAYERS - 1) + 2;
int randomizeGameState(struct gameState *state){
	
	//Randomize number of players between 2 and 4
	state->numPlayers = RANDOM_PLAYER_COUNT_EVERYWHERE; 
	//state->numPlayers = 2;
	
	//int players = state->numPlayers;
	
	//Randomize whose turn it is
	state->whoseTurn = rand()%state->numPlayers;
	
	//Randomize played card count
	state->playedCardCount = rand()%MAX_HAND;
  	
  	//Randomize number of cards in deck, discard pile
  	int n; 	
  	for(n = 0; n < state->numPlayers; n++){ //fill a random hand
		state->deckCount[n] = rand()%MAX_DECK;
		state->discardCount[n] = rand()%MAX_DECK;
		state->handCount[n] = 0;
		
	}
	

	//Randomize hand count for current player
	state->handCount[state->whoseTurn] = rand()%MAX_HAND;
	//printf("hand count for curent player: %d\n", state->handCount[state->whoseTurn]);

  	return 0;
}

int ramdomizeAdventurer(struct gameState *state){
	
	int card;
	int choice1, choice2, choice3;
	int handPos = 0;
	int bonus;
	int failure = 0;
	
	//printf("%d\n", state->whoseTurn);
	
	//printf("player's turn: %d\n", state->whoseTurn);
	//printf("number of players: %d\n", state->numPlayers);
	handPos = rand()%state->handCount[state->whoseTurn];
	state->hand[state->whoseTurn][handPos] = adventurer;

	//printf("%d\n", handPos);
	//printf("%d\n", state->hand[state->whoseTurn][handPos]);
	  
	int handCountBefore = state->handCount[state->whoseTurn];
	int playedCountBefore = state->playedCardCount;
	
	cardEffect(adventurer, 1, 1, 1, state, handPos, 0);
	
	
	int handCountAfter = state->handCount[state->whoseTurn];
	int playedCountAfter = state->playedCardCount;

	
	if(state->hand[state->whoseTurn][handCountBefore] == 4 || state->hand[state->whoseTurn][handCountBefore] == 5 || state->hand[state->whoseTurn][handCountBefore] == 6){
		//do nothing
	} else {
		failure ++;
		printf("TEST FAILED: Second to last treasure is not a coin\n"); //Fails, one is not discarded at the end
	}

	if(state->hand[state->whoseTurn][handCountBefore + 1]== 4 || state->hand[state->whoseTurn][handCountBefore] == 5 || state->hand[state->whoseTurn][handCountBefore] == 6){
	//do nothing
	} else {
		failure ++;
	printf("TEST FAILED: Last card is a treasure\n"); //Fails, one is not discarded at the end
	}

	if(handCountAfter - handCountBefore == 2){
	//do nothing
	} else {
		failure ++;
	printf("TEST FAILED: Hand Count after adventurer is %d\n", handCountAfter); //Fails, one is not discarded at the end
	}

	//One card should be played 
	if(playedCountBefore - playedCountAfter == 0){
	//do nothing
	} else {
		failure ++;
		printf("TEST FAILED: Number of cards played: %d\n", playedCountAfter); //Fails, card is not played at the end
	}	
  	printf("Number of unit tests that failed: %d \n", failure);
  	return failure;
}

int main() {
	srand(time(NULL));
	int total_failed = 0;
	
	int i;
	for(i = 0; i < NUM_TESTS; i++){
		//randomize the seed
		int seed = rand()%10000;
		//int   RANDOM_PLAYER_COUNT_EVERYWHERE=;  //Gets randomly changed later.

		int k[10] = {adventurer, council_room, feast, gardens, mine,
			   remodel, smithy, village, baron, great_hall};

		struct gameState G;
	
		initializeGame( RANDOM_PLAYER_COUNT_EVERYWHERE, k, seed, &G);
	
		randomizeGameState(&G);
		total_failed = total_failed + ramdomizeAdventurer(&G);
		
		
	}
	
	printf("total failed tests: %d\n", total_failed);
	
	return 0;

}
